# -*- coding: utf8 -*-


import time
import json
import pymongo

from scrapy import Spider
from scrapy.http import Request


class Virtual(Spider):
    name = 'virtual'

    def __init__(self, name=None, **kwargs):
        super(Virtual, self).__init__(name, **kwargs)

        # 连接mongodb
        client = pymongo.MongoClient('47.254.26.182', 27017)
        self.db = client.bt_news

    def start_requests(self):
        cach = 1
        token = 2
        coin_type = [cach, token]
        for pairs in coin_type:
            for page in range(0, 30):
                # https://block.cc/api/v1/coin/list?page=0&size=50&select=marketCap&orderby=-1&coinType=1
                url = 'https://block.cc/api/v1/coin/list?page=%d&size=50&select=marketCap&orderby=-1&coinType=%s' % (page, pairs)

                yield Request(
                    url=url,
                    dont_filter=True,
                    headers={
                        'Host': 'block.cc',
                        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36',
                    },
                    meta={
                        'coin_type': pairs,
                    },
                    callback=self.coin_info
                )

    def coin_info(self, response):
        body = json.loads(response.body)

        # 货币类型（1货币，2代币）
        coin_type = response.meta.get('coin_type')

        data = body.get('data')
        info_list = data.get('list')

        if list:
            for info in info_list:
                # 中文昵称
                name_cn = info.get('name')

                # 显示名称
                display_name = info.get('symbol')

                # 流通市值
                # market_value = info.get('available_supply')

                # 平台价格
                price = info.get('price')

                # 交易量(24H)（币）
                volume = info.get('volume_ex')

                # 每小时涨幅
                change_hourly = info.get('change1h')

                # 每天涨幅
                change_daily = info.get('change1d')

                # 每周涨幅
                change_weekly = info.get('change7d')

                # 流通数量
                total_volume = info.get('available_supply')

                # 发行总量
                total_num = info.get('marketCap')

                # 官网地址
                home_url = 'https://%s.com/' % display_name

                # 简介
                # commets

                # 入库
                data = {
                    'name_cn': name_cn,
                    'display_name': display_name,
                    'coin_type': coin_type,
                    # 'market_value': market_value,
                    'addtime': time.localtime(),
                    'price': price,
                    'volume': volume,
                    'change_hourly': change_hourly,
                    'change_daily': change_daily,
                    'change_weekly': change_weekly,
                    'total_volume': total_volume,
                    'total_num': total_num,
                    'home_url': home_url,
                    # 'commets': commets,
                }

                # 存入到mongo中
                self.db.bt_virtual_coin.update_one({'display_name': display_name}, {'$set': data}, upsert=True)

# 数据库结构
'''

'''